package com.taobao.txc.datasource.cobar;

import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.j;
import com.taobao.txc.common.util.c.g;
import com.taobao.txc.parser.struct.TxcRuntimeContext;
import com.taobao.txc.resourcemanager.b.d;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class a
  extends com.taobao.txc.resourcemanager.b.c.c
{
  private static final LoggerWrap d = LoggerInit.logger;
  
  public a(Connection paramConnection, com.taobao.txc.resourcemanager.b.a.b paramb)
  {
    super(paramConnection, paramb);
  }
  
  public int a(String paramString, int paramInt1, int paramInt2)
  {
    if (paramInt2 < 1) {
      return 0;
    }
    StringBuilder localStringBuilder = new StringBuilder("DELETE FROM ");
    localStringBuilder.append(paramString);
    String str = g.a(paramInt1);
    PreparedStatement localPreparedStatement = null;
    int i = 0;
    try
    {
      if (d() != d.a)
      {
        localStringBuilder.append(" WHERE gmt_modified < '").append(str).append("'");
        localStringBuilder.append(" LIMIT ").append(paramInt2);
        localPreparedStatement = b().prepareStatement(localStringBuilder.toString());
        i = localPreparedStatement.executeUpdate();
      }
      else
      {
        localStringBuilder.append(" WHERE gmt_modified < to_date('").append(str).append("','yyyy-mm-dd')");
        localPreparedStatement = b().prepareStatement(localStringBuilder.toString());
        i = localPreparedStatement.executeUpdate();
      }
    }
    catch (Exception localException)
    {
      if ((localException instanceof SQLException)) {
        throw ((SQLException)localException);
      }
      throw new SQLException(localException);
    }
    finally
    {
      if (localPreparedStatement != null) {
        localPreparedStatement.close();
      }
    }
    return i;
  }
  
  public void a(TxcRuntimeContext paramTxcRuntimeContext, String paramString)
  {
    if (paramTxcRuntimeContext == null)
    {
      d.error("", "txcLog is null");
      return;
    }
    String str1 = paramTxcRuntimeContext.a();
    long l = paramTxcRuntimeContext.b();
    String str2 = paramTxcRuntimeContext.e();
    d locald = d();
    StringBuilder localStringBuilder;
    PreparedStatement localPreparedStatement;
    Object localObject1;
    if (locald != d.a)
    {
      localStringBuilder = new StringBuilder("INSERT INTO ");
      localStringBuilder.append(paramString);
      localStringBuilder.append("(id, xid, branch_id, rollback_info, ");
      localStringBuilder.append("gmt_create, gmt_modified, status, server)");
      localStringBuilder.append(" VALUES(");
      localStringBuilder.append("?,");
      localStringBuilder.append("?,");
      localStringBuilder.append("?,");
      localStringBuilder.append("?,");
      localStringBuilder.append("now(),");
      localStringBuilder.append("now(),");
      localStringBuilder.append(paramTxcRuntimeContext.d());
      localStringBuilder.append(",?)");
      localPreparedStatement = null;
      try
      {
        localPreparedStatement = b().prepareStatement(localStringBuilder.toString());
        localPreparedStatement.setLong(1, l);
        localPreparedStatement.setString(2, str1);
        localPreparedStatement.setLong(3, l);
        localPreparedStatement.setBlob(4, com.taobao.txc.common.util.a.a.a(paramTxcRuntimeContext.encode()));
        localPreparedStatement.setString(5, str2);
        localPreparedStatement.executeUpdate();
        if (d.c())
        {
          d.debug(paramTxcRuntimeContext.a());
          d.debug("" + paramTxcRuntimeContext.b());
          d.debug("size:" + paramTxcRuntimeContext.c().size());
          Iterator localIterator1 = paramTxcRuntimeContext.c().iterator();
          while (localIterator1.hasNext())
          {
            localObject1 = (com.taobao.txc.parser.struct.c)localIterator1.next();
            d.debug(((com.taobao.txc.parser.struct.c)localObject1).toString());
          }
          d.debug(paramTxcRuntimeContext.encode());
        }
      }
      catch (Exception localException1)
      {
        d.a(String.format("%s:%d %s", new Object[] { str1, Long.valueOf(l), paramTxcRuntimeContext.encode() }), com.taobao.txc.common.b.b.an, localException1);
        if ((localException1 instanceof SQLException)) {
          throw ((SQLException)localException1);
        }
        throw new SQLException(localException1);
      }
      finally
      {
        if (localPreparedStatement != null) {
          localPreparedStatement.close();
        }
        d.info(String.format("[%d:%d] insertUndoLog cost %d ms", new Object[] { Long.valueOf(j.a(str1)), Long.valueOf(l), Long.valueOf(paramTxcRuntimeContext.i()) }));
      }
    }
    else
    {
      localStringBuilder = new StringBuilder("INSERT INTO ");
      localStringBuilder.append(paramString);
      localStringBuilder.append("(id, xid, branch_id, rollback_info, ");
      localStringBuilder.append("gmt_create, gmt_modified, status, server)");
      localStringBuilder.append(" VALUES(");
      localStringBuilder.append("?,");
      localStringBuilder.append("?,");
      localStringBuilder.append("?,");
      localStringBuilder.append("empty_blob(),");
      localStringBuilder.append("sysdate,");
      localStringBuilder.append("sysdate,");
      localStringBuilder.append(paramTxcRuntimeContext.d());
      localStringBuilder.append(",?)");
      localPreparedStatement = null;
      ResultSet localResultSet = null;
      localObject1 = null;
      try
      {
        localPreparedStatement = b().prepareStatement(localStringBuilder.toString());
        localPreparedStatement.setLong(1, l);
        localPreparedStatement.setString(2, str1);
        localPreparedStatement.setLong(3, l);
        localPreparedStatement.setString(4, str2);
        localPreparedStatement.executeUpdate();
        String str3 = "SELECT rollback_info FROM " + paramString + " WHERE id = " + l + " FOR UPDATE";
        localPreparedStatement = b().prepareStatement(str3);
        localResultSet = localPreparedStatement.executeQuery();
        if (localResultSet.next()) {
          localObject1 = localResultSet.getBlob(1);
        }
        OutputStream localOutputStream = (OutputStream)localObject1.getClass().getMethod("setBinaryStream", new Class[] { Long.TYPE }).invoke(localObject1, new Object[] { Long.valueOf(1L) });
        String str4 = paramTxcRuntimeContext.encode();
        byte[] arrayOfByte = str4.getBytes();
        localOutputStream.write(arrayOfByte, 0, arrayOfByte.length);
        localOutputStream.close();
        if (d.c())
        {
          d.debug(paramTxcRuntimeContext.a());
          d.debug("" + paramTxcRuntimeContext.b());
          d.debug("size:" + paramTxcRuntimeContext.c().size());
          Iterator localIterator2 = paramTxcRuntimeContext.c().iterator();
          while (localIterator2.hasNext())
          {
            com.taobao.txc.parser.struct.c localc = (com.taobao.txc.parser.struct.c)localIterator2.next();
            d.debug(localc.toString());
          }
          d.debug(paramTxcRuntimeContext.encode());
        }
      }
      catch (Exception localException2)
      {
        d.a(String.format("%s:%d %s", new Object[] { str1, Long.valueOf(l), paramTxcRuntimeContext.encode() }), com.taobao.txc.common.b.b.an, localException2);
        if ((localException2 instanceof SQLException)) {
          throw ((SQLException)localException2);
        }
        throw new SQLException(localException2);
      }
      finally
      {
        if (localResultSet != null) {
          localResultSet.close();
        }
        if (localPreparedStatement != null) {
          localPreparedStatement.close();
        }
        d.info(String.format("[%d:%d] insertUndoLog cost %d ms", new Object[] { Long.valueOf(j.a(str1)), Long.valueOf(l), Long.valueOf(paramTxcRuntimeContext.i()) }));
      }
    }
  }
  
  public String a(com.taobao.txc.resourcemanager.b.a.a parama, String paramString)
  {
    if (StringUtils.isEmpty(paramString))
    {
      d.error("", "txc sql is null");
      return null;
    }
    Blob localBlob = null;
    PreparedStatement localPreparedStatement = null;
    ResultSet localResultSet = null;
    try
    {
      localPreparedStatement = parama.b().prepareStatement(paramString);
      localResultSet = localPreparedStatement.executeQuery();
      if (localResultSet.next()) {
        localBlob = localResultSet.getBlob("rollback_info");
      }
    }
    catch (Exception localException1)
    {
      if ((localException1 instanceof SQLException)) {
        throw ((SQLException)localException1);
      }
      throw new SQLException(localException1);
    }
    finally
    {
      if (localResultSet != null) {
        localResultSet.close();
      }
      if (localPreparedStatement != null) {
        localPreparedStatement.close();
      }
    }
    if (d() != d.a) {
      return com.taobao.txc.common.util.a.a.a(localBlob);
    }
    if (localBlob == null) {
      return null;
    }
    try
    {
      InputStream localInputStream = (InputStream)localBlob.getClass().getMethod("getBinaryStream", new Class[0]).invoke(localBlob, new Object[0]);
      String str = com.taobao.txc.common.util.a.a.a(localInputStream);
      localInputStream.close();
      return str;
    }
    catch (Exception localException2)
    {
      localException2.printStackTrace();
    }
    return null;
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.datasource.cobar.a
 * JD-Core Version:    0.7.0.1
 */